package com.itheima.reggie.aop;


import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.time.LocalDateTime;

/*@Aspect
@Slf4j
@Component
public class CommonColumnAdvice {

    public CommonColumnAdvice(){
        log.info("===================================");
    }

    //对哪些类或者方法进行增强 切点表达式
    @Pointcut(
            "execution(* com.itheima.reggie.controller.*Controller.update*(..))" +
                    " || " +
                    " execution(* com.itheima.reggie.controller.*Controller.save*(..))")
    public void pt(){}

    //增强的功能 ，对四个字段的值进行自动填充！！
    @Around("pt()")
    public Object modifyColumn(ProceedingJoinPoint pjp) throws Throwable {
        log.info("aop 执行了....");
        Object proceed = null;

            // 拿到 Employee 参数对象
            Object[] args = pjp.getArgs();
            Object obj = args[0];// Employee Category ...
            Class<?> aClass = obj.getClass();
            String methodName = pjp.getSignature().getName();
            if(methodName.startsWith("save")){//新增
                Method setCreateTime =
                        aClass.getMethod("setCreateTime", LocalDateTime.class);
                setCreateTime.invoke(obj,LocalDateTime.now());
                //setCreateTime(LocatDateTime.now())
                Method setCreateUser =
                        aClass.getMethod("setCreateUser", Long.class);
                setCreateUser.invoke(obj,1L);

            }
            Method setUpdateTime =
                    aClass.getMethod("setUpdateTime", LocalDateTime.class);
            setUpdateTime.invoke(obj,LocalDateTime.now());
            Method setUpdateUser =
                    aClass.getMethod("setUpdateUser", Long.class);
            setUpdateUser.invoke(obj,1L);
            // set 设置它那四个值
            proceed = pjp.proceed(args);

        return proceed;
    }
}*/
